<?php
declare(strict_types=1);

namespace app\admin\controller;

use app\admin\service\ResourceCheckService;
use app\admin\service\OrderService;
use app\admin\service\ArticleService;
use app\admin\service\ArticleViewsService;
use app\admin\service\ResourceService;
use app\admin\service\UsersCateService;
use app\admin\service\UsersService;
use app\admin\service\UsersAccessLogService;
use app\admin\service\UsersMemberService;
use app\AdminController;
use think\facade\View;

/**
 * 数据分析
 * Class Analysis
 * @package app\admin\controller
 */
class Analysis extends AdminController
{
    /**
     * 用户数据
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function usersData()
    {
        // 用户总数
        $usersTotal = UsersService::countUsers();
        // 本周新增
        $usersWeekCreate = UsersService::countUsersWeekCreate();
        // 昨日新增
        $yesTodayUsers = UsersService::yesTodayUsers();
        // 实时新增
        $todayUsers = UsersService::todayUsers();
        // 日活跃度
        $todayBriskUsers = UsersAccessLogService::todayBriskUsers();
        // 日访问
        $todayAccess = UsersAccessLogService::todayAccess();
        // 小程序用户
        $appletsWhere = ['associate' => 2];
        $appletsUsers = UsersService::countUsers($appletsWhere);
        // 小程序日活跃度用户
        $appletsTodayBriskUsers = UsersAccessLogService::todayBriskUsers($appletsWhere);
        // 小程序昨日新增
        $appletsTodayCreateUsers = UsersService::yesTodayUsers($appletsWhere);
        // H5用户
        $h5Where = ['associate' => 1];
        $h5Users = UsersService::countUsers($h5Where);
        // H5日活跃度用户
        $h5TodayBriskUsers = UsersAccessLogService::todayBriskUsers($h5Where);
        // H5昨日新增
        $h5TodayCreateUsers = UsersService::yesTodayUsers($h5Where);
        // 总会员
        $countVipUsers = UsersMemberService::countUsersMember();
        // 108年会员
        $vipYearsUsers = UsersMemberService::countUsersMember(['mid' => 1]);
        // 8.8月会员
        $vipMonthUsers = UsersMemberService::countUsersMember(['mid' => 3]);
        // 用户地区分布
        $usersArea = UsersService::getUsersArea();
        // 身份数据
        $usersIdentity = UsersService::getUsersIdentity();

        View::assign([
            'users_data' => [
                'usersTotal' => $usersTotal,
                'usersWeekCreate' => $usersWeekCreate,
                'yesTodayUsers' => $yesTodayUsers,
                'todayUsers' => $todayUsers,
                'todayBriskUsers' => $todayBriskUsers,
                'todayAccess' => $todayAccess,
            ],
            'port_data' => [
                'appletsUsers' => $appletsUsers,
                'appletsLastUsers' => $usersTotal - $appletsUsers,
                'appletsUsersScale' => round(($appletsUsers / $usersTotal) * 100, 2) . '%',
                'appletsTodayBriskUsers' => $appletsTodayBriskUsers,
                'appletsTodayCreateUsers' => $appletsTodayCreateUsers,
                'h5Users' => $h5Users,
                'h5LastUsers' => $usersTotal - $h5Users,
                'h5UsersScale' => round(($h5Users / $usersTotal) * 100, 2) . '%',
                'h5TodayBriskUsers' => $h5TodayBriskUsers,
                'h5TodayCreateUsers' => $h5TodayCreateUsers,
            ],
            'member_data' => [
                'vipLastMonthUsers' => $countVipUsers - $vipMonthUsers,
                'vipMonthUsers' => $vipMonthUsers,
                'vipLastYearsUsers' => $countVipUsers - $vipYearsUsers,
                'vipYearsUsers' => $vipYearsUsers,
            ],
            'usersArea' => $usersArea,
            'usersIdentity' => $usersIdentity,
        ]);

        return View::fetch('users');
    }

    /**
     * 社群数据
     * @return string
     */
    public function communityData()
    {
        // 当日新增
        $todayResource = ResourceService::todayResource();
        // 社群分类
        $resourceCate = ResourceService::resourceCate();
        // 总社群
        $countResource = ResourceService::countResource();
        // 当月总社群
        $countResourceMonth = ResourceService::countResourceMonth();
        // 当月社群
        $resourceMonthLine = ResourceService::getResourceMonthLine();
        // 社群分布地区
        $resourceArea = ResourceService::getResourceArea();
        // 优质社群
        $countHighResource = ResourceService::countResource(['is_high' => 1]);
        // 服务商
        $countServiceResource = ResourceService::countResource(['is_service' => 1]);
        // 合作伙伴
        $countPartnerResource = ResourceService::countResource(['is_partner' => 1]);
        // 审核中
        $resourceCheck = ResourceCheckService::countResourceCheck(['status' => 0]);
        // 已通过
        $resourcePass = ResourceCheckService::countResourceCheck(['status' => 1]);
        // 被驳回
        $resourceReject = ResourceCheckService::countResourceCheck(['status' => 2]);

        View::assign([
            'resource_data' => [
                'resourceMonthLine' => $resourceMonthLine,
                'countResource' => $countResource,
                'countResourceMonth' => $countResourceMonth,
            ],
            'resourceArea' => $resourceArea,
            'cate_data' => [
                'todayResource' => $todayResource,
                'resourceCate' => [
                    'resourceCate' => $resourceCate['resourceCate'],
                    'line_data' => $resourceCate['line_data'],
                ],
            ],
            'work_data' => [
                'countHighResource' => $countHighResource,
                'countServiceResource' => $countServiceResource,
                'countPartnerResource' => $countPartnerResource,
            ],
            'check_data' => [
                'resourceCheck' => $resourceCheck,
                'resourceLastCheck' => $countResource - $resourceCheck,
                'resourceCheckScale' => round(($resourceCheck / $countResource) * 100, 2) . '%',
                'resourcePass' => $resourcePass,
                'resourceLastPass' => $countResource - $resourcePass,
                'resourcePassScale' => round(($resourcePass / $countResource) * 100, 2) . '%',
                'resourceReject' => $resourceReject,
                'resourceLastReject' => $countResource - $resourceReject,
                'resourceRejectScale' => round(($resourceReject / $countResource) * 100, 2) . '%',
            ]
        ]);

        return View::fetch('community');
    }

    /**
     * 涨知识数据
     * @return string
     */
    public function loreData()
    {
        // 企业微信
        $weChatWhere = ['types' => 1];
        $weChatArticleMonthLine = ArticleService::getArticleMonthLine($weChatWhere);
        $weChatCountArticle = ArticleService::countArticle($weChatWhere);
        $weChatArticleMonthCreate = ArticleService::countArticleMonthCreate($weChatWhere);
        $weChatArticleCountViews = ArticleViewsService::countArticleViewsNums($weChatWhere);
        $weChatArticleTodayCountViews = ArticleViewsService::countTodayArticleViewsNums($weChatWhere);
        $weChatArticleMonthCountViews = ArticleViewsService::countMonthArticleViewsNums($weChatWhere);

        // 社群知识
        $loreWhere = ['types' => 2];
        $loreArticleMonthLine = ArticleService::getArticleMonthLine($loreWhere);
        $loreCountArticle = ArticleService::countArticle($loreWhere);
        $loreArticleMonthCreate = ArticleService::countArticleMonthCreate($loreWhere);
        $loreArticleCountViews = ArticleViewsService::countArticleViewsNums($loreWhere);
        $loreArticleTodayCountViews = ArticleViewsService::countTodayArticleViewsNums($loreWhere);
        $loreArticleMonthCountViews = ArticleViewsService::countMonthArticleViewsNums($loreWhere);

        // 社群案例
        $caseWhere = ['types' => 3];
        $caseArticleMonthLine = ArticleService::getArticleMonthLine($caseWhere);
        $caseCountArticle = ArticleService::countArticle($caseWhere);
        $caseArticleMonthCreate = ArticleService::countArticleMonthCreate($caseWhere);
        $caseArticleCountViews = ArticleViewsService::countArticleViewsNums($caseWhere);
        $caseArticleTodayCountViews = ArticleViewsService::countTodayArticleViewsNums($caseWhere);
        $caseArticleMonthCountViews = ArticleViewsService::countMonthArticleViewsNums($caseWhere);

        View::assign([
            'weChat_data' => [
                'weChatArticleMonthLine' => $weChatArticleMonthLine,
                'weChatCountArticle' => $weChatCountArticle,
                'weChatArticleMonthCreate' => $weChatArticleMonthCreate,
                'weChatArticleCountViews' => $weChatArticleCountViews,
                'weChatArticleTodayLastCountViews' => $weChatArticleCountViews - $weChatArticleTodayCountViews,
                'weChatArticleTodayCountViews' => $weChatArticleTodayCountViews,
                'weChatArticleMonthCountViews' => $weChatArticleMonthCountViews,
            ],
            'lore_data' => [
                'loreArticleMonthLine' => $loreArticleMonthLine,
                'loreCountArticle' => $loreCountArticle,
                'loreArticleMonthCreate' => $loreArticleMonthCreate,
                'loreArticleCountViews' => $loreArticleCountViews,
                'loreArticleTodayLastCountViews' => $loreArticleCountViews - $loreArticleTodayCountViews,
                'loreArticleTodayCountViews' => $loreArticleTodayCountViews,
                'loreArticleMonthCountViews' => $loreArticleMonthCountViews,
            ],
            'case_data' => [
                'caseArticleMonthLine' => $caseArticleMonthLine,
                'caseCountArticle' => $caseCountArticle,
                'caseArticleMonthCreate' => $caseArticleMonthCreate,
                'caseArticleCountViews' => $caseArticleCountViews,
                'caseArticleTodayLastCountViews' => $caseArticleCountViews - $caseArticleTodayCountViews,
                'caseArticleTodayCountViews' => $caseArticleTodayCountViews,
                'caseArticleMonthCountViews' => $caseArticleMonthCountViews,
            ],
        ]);

        return View::fetch('lore');
    }

    /**
     * 需求数据
     * @return string
     */
    public function demandData()
    {
        return View::fetch('demand');
    }

    /**
     * 订单数据
     * @return string
     */
    public function orderData()
    {
        // 8.8月订单
        $orderMonth = OrderService::getOrderMonthPrice();
        // 108年订单
        $orderYear = OrderService::getOrderYearPrice();
        // 社群礼包订单
        $orderResource = OrderService::getOrderResourcePrice();
        // 订阅需求订单
        $orderDemand = OrderService::getOrderDemandPrice();
        // 订单地区分布
        $orderArea = OrderService::getOrderArea();
        // 市场需求数据
        $demandData = OrderService::getDemandData();

        View::assign([
            'orderMonth' => $orderMonth,
            'orderYear' => $orderYear,
            'orderResource' => $orderResource,
            'orderDemand' => $orderDemand,
            'orderArea' => $orderArea,
            'demandData' => $demandData,
        ]);

        return View::fetch('order');
    }
}